import { useState } from 'react';

type useCurrentSideReturns = [
  string[],
  string[],
  (currentOpenKeys: string[]) => void,
  (currentOpenKeys: string[]) => void
]

export const useCurrentSide = (originOpenKeys: string[], originOpenKeysRootSubmenuKeys: string[])
  : useCurrentSideReturns => {
  // 对demo等进行了一些封装
  const [openKeys, changeOpenKeys] = useState<string[]>(originOpenKeys);
  const [rootSubmenuKeys, changeRootSubmenuKeys] = useState<string[]>(originOpenKeysRootSubmenuKeys);

  const handleOpenChange = (currentOpenKeys: string[]) => {
    const latestOpenKey = currentOpenKeys.find(key => openKeys.indexOf(key) === -1);
    if (rootSubmenuKeys.indexOf(latestOpenKey!) === -1) {
      changeOpenKeys(currentOpenKeys);
    } else {
      changeOpenKeys(latestOpenKey ? [latestOpenKey] : []);
    }
  };

  return [openKeys, rootSubmenuKeys, handleOpenChange, changeRootSubmenuKeys];
};